package com.lcredu.web;/**
 * @author 黎长容
 * @date 2025-07-05 18:55
 */

import com.lcredu.entity.Member;
import com.lcredu.service.MemberService;
import com.lcredu.service.impl.MemberServiceImpl;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet(name = "MemberServlet", urlPatterns = "/MemberServlet")
public class MemberServlet extends BasicServlet {
    MemberService memberService = new MemberServiceImpl();

//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //这里我们合并一下
//        String action = request.getParameter("action");
//        System.out.println(action);
//        if ("login".equals(action)) {
//            login(request, response);
//        } else if ("register".equals(action)) {
//            register(request, response);
//        }else {
//            response.getWriter().write("请求参数action有误...");
//        }
//
//    }


    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //用户登录
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Member user = memberService.login(new Member(null, username, password, null));
        if (user != null) {
            System.out.println("ok!!!");
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            request.getRequestDispatcher("/views/member/login_ok.jsp").forward(request, response);
        } else {
            //登陆失败
            request.setAttribute("member", user);
            request.setAttribute("errorMsg", "用户名或密码错误");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        String code = request.getParameter("code");

        //在这里判断验证码
        String token = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //立即删除验证码,防止重复使用
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token != null && code.equalsIgnoreCase(token)) {
            //判断用户名是否可用
            if (memberService.getMemberByName(username)) {
                //如果可用
                Member member = new Member(null, username, password, email);
                System.out.println(member);
                if (memberService.savMember(member)) {
                    //注册成功
                    request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request, response);
                } else {
                    //注册失败
                    request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request, response);
                }
            } else {
                //不可用直接返回主页面
                request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
            }
        } else {
            //验证码不正确
            request.setAttribute("errorMsg", "验证码不正确");
            //带回一个信息,要显示到注册选项页
            request.setAttribute("active", "register");
            //回显数据
            request.setAttribute("username", username);
            request.setAttribute("email", email);
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }


    }

    protected void loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        HttpSession session = request.getSession();
//        Member user = (Member) session.getAttribute("user");
//        if (user != null) {
//            session.removeAttribute("user");
//        }
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath());

    }
}